package com.cqeec.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.cqeec.domain.Manager;
import com.cqeec.domain.Student;
import com.cqeec.domain.Teacher;
import com.cqeec.service.ManagerService;
import com.cqeec.service.StudentService;
import com.cqeec.service.TeacherService;
import com.cqeec.service.imp.ManagerServicelmpl;
import com.cqeec.service.imp.StudentServiceImpl;
import com.cqeec.service.imp.TeacherServiceImpl;
import com.cqeec.utils.JDBCUtils;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JRadioButton;
import java.awt.Font; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton; 
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.util.Enumeration;
import java.awt.event.ActionEvent; 
import java.awt.Color;

/**
 * 学生登录界面
 * 
 * @author 邓学强
 *
 */
public class Login extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static JPanel contentPane;
	private JRadioButton rdbtnNewRadioButton;
	private JRadioButton rdbtnNewRadioButton_1;
	private JRadioButton radioButton;
	private static JLabel label;
	private static Login frame;
	private JTextField textField;
	private JPasswordField passwordField;
	private JButton button_1;
	private Connection conn;
	private StudentInterface st;
	private TeacherInterface teacherInterface;


	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {

					frame = new Login();
				
					String path = "";
					// 背景图片
					ImageIcon background = new ImageIcon(path);
					// 把背景图片显示在一个标签里面
					label = new JLabel(background);

					// 把标签的大小位置设置为图片刚好填充整个面板
					label.setBounds(0, 0, 1162, 524);
					// 把内容窗格转化为JPanel，否则不能用方法setOpaque()来使内容窗格透明
					JPanel imagePanel = (JPanel) frame.getContentPane();
					imagePanel.setOpaque(false);
					// 把背景图片添加到分层窗格的最底层作为背景
					frame.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
					frame.setResizable(false);// 设置窗体不可拖动
					frame.setVisible(true);

				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Login() {
		setTitle("登录");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1170, 545);
		contentPane = new JPanel();

		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		rdbtnNewRadioButton = new JRadioButton("学生");
		rdbtnNewRadioButton.setSelected(true);
		rdbtnNewRadioButton.setForeground(Color.GREEN);
		rdbtnNewRadioButton.setOpaque(false);
		rdbtnNewRadioButton.setFont(new Font("宋体", Font.BOLD, 23));

		rdbtnNewRadioButton_1 = new JRadioButton("教师");
		rdbtnNewRadioButton_1.setForeground(Color.GREEN);
		rdbtnNewRadioButton_1.setOpaque(false);
		rdbtnNewRadioButton_1.setFont(new Font("宋体", Font.BOLD, 23));

		radioButton = new JRadioButton("管理员");
		radioButton.setForeground(new Color(0, 255, 0));
		radioButton.setOpaque(false);
		radioButton.setFont(new Font("宋体", Font.BOLD, 23));
		ButtonGroup group = new ButtonGroup();
		group.add(this.rdbtnNewRadioButton);
		group.add(this.rdbtnNewRadioButton_1);
		group.add(this.radioButton);

		JLabel label = new JLabel("用户名：");
		label.setFont(new Font("黑体", Font.BOLD, 25));

		JLabel label_1 = new JLabel("密 码：");
		label_1.setFont(new Font("黑体", Font.BOLD, 25));

		textField = new JTextField();
		textField.setOpaque(false);
		textField.setFont(new Font("宋体", Font.PLAIN, 18));
		textField.setColumns(10);

		passwordField = new JPasswordField();
		passwordField.setOpaque(false);
		passwordField.setFont(new Font("宋体", Font.PLAIN, 18));

		JButton button = new JButton("登录");
		button.setOpaque(false);
		button.addActionListener(new ActionListener() {
			/*
			 * 登录按钮事件
			 * 
			 * @see
			 * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
			 */
			public void actionPerformed(ActionEvent arg0) {
				conn = JDBCUtils.getConnection();

				if (conn == null) {
					JOptionPane.showMessageDialog(frame, "链接数据库错误 请联系管理员", "错误", JOptionPane.ERROR_MESSAGE);
					return;
				}

				// 获取账号和密码
				String user = textField.getText().trim();
				String password = new String(passwordField.getPassword(), 0, passwordField.getPassword().length);
				
				
				if(user.equals("")) {
					JOptionPane.showMessageDialog(null, "请输入用户id", "提示", JOptionPane.ERROR_MESSAGE);
					textField.requestFocus();
					return;
				}
				
				if(password.equals("")) {
					JOptionPane.showMessageDialog(null, "请输入用户密码", "提示", JOptionPane.ERROR_MESSAGE);
					passwordField.requestFocus();
					return;
				} 
				
				// 获取单选按钮的值
				Enumeration<AbstractButton> bu = group.getElements();

				while (bu.hasMoreElements()) {
					AbstractButton btn = bu.nextElement();
					if (btn.isSelected()) {
						if (btn.getText().trim().equals("学生")) {
							Student student = new Student();
							student.setStudentNum(user);
							student.setStudentPwd(password);
							StudentService studentService = new StudentServiceImpl();
							Student myStudent = studentService.findStudentByID(conn, student);
						
							if (myStudent != null) { 
								frame.setVisible(false);
							
								
							
								StudentInterface.main(myStudent);
							
							}
						}
						if (btn.getText().trim().equals("教师")) {
							Teacher teacher = new Teacher(); 
							teacher.setTeacherNum(user);
							teacher.setTeacherPwd(password);
							TeacherService teacherService = new TeacherServiceImpl();
							Teacher myTeacher  = teacherService.findTeacherByID(conn,teacher);
							if (myTeacher != null) { 
								frame.setVisible(false);
								
								TeacherInterface.main(myTeacher);
							
							}
						}
						if (btn.getText().trim().equals("管理员")) {
                        Manager manager=new Manager();
                        manager.setManagerNum(user);
                        manager.setManagerPwd(password);
                        ManagerService managerService=new ManagerServicelmpl();
                        Manager  myManager = managerService.findManagerByID(conn, manager);
                        if(myManager!=null) {
                        	
                        	frame.setVisible(false);
							MainInterface mainInterface=new MainInterface();
							mainInterface.main(null);
						};
                     
						}
						break;
					}
				}

			}
		});
		button.setFont(new Font("宋体", Font.PLAIN, 22));

		button_1 = new JButton("重置");
		button_1.setOpaque(false);
		button_1.addActionListener(new ActionListener() {
			/**
			 * 重置按钮事件
			 */
			public void actionPerformed(ActionEvent e) {
				textField.setText("");
				passwordField.setText("");
				

			}
		});
		button_1.setFont(new Font("宋体", Font.PLAIN, 22));

		JLabel label_2 = new JLabel("高  校  点  名  系  统");
		label_2.setFont(new Font("华文彩云", Font.BOLD, 30));

		
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING).addGroup(gl_contentPane
				.createSequentialGroup().addContainerGap(708, Short.MAX_VALUE)
				.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_contentPane.createSequentialGroup()
								.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING).addComponent(label_1)
										.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
												.addComponent(rdbtnNewRadioButton).addComponent(label)))
								.addGap(18)
								.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false)
										.addComponent(textField, Alignment.LEADING).addComponent(passwordField,
												Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE))
								.addGap(133))
						.addGroup(gl_contentPane.createSequentialGroup().addComponent(rdbtnNewRadioButton_1).addGap(53)
								.addComponent(radioButton).addGap(101))))
				.addGroup(gl_contentPane.createSequentialGroup().addGap(186).addComponent(label_2).addContainerGap(776,
						Short.MAX_VALUE))
				.addGroup(gl_contentPane.createSequentialGroup().addContainerGap(752, Short.MAX_VALUE)
						.addComponent(button).addGap(110).addComponent(button_1).addGap(126)));
		gl_contentPane
				.setVerticalGroup(
						gl_contentPane.createParallelGroup(Alignment.TRAILING)
								.addGroup(
										gl_contentPane.createSequentialGroup().addGap(40).addComponent(label_2)
												.addGap(51)
												.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
														.addComponent(textField, GroupLayout.PREFERRED_SIZE, 49,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(label))
												.addGap(28)
												.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
														.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, 42,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(label_1))
												.addGap(33)
												.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
														.addComponent(radioButton).addComponent(rdbtnNewRadioButton_1)
														.addComponent(rdbtnNewRadioButton))
												.addPreferredGap(ComponentPlacement.RELATED, 62, Short.MAX_VALUE)
												.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
														.addComponent(button).addComponent(button_1))
												.addGap(80)));
		contentPane.setLayout(gl_contentPane);
	}
}
